package com.tang.net;

import com.tang.base.BaseView;
import com.google.gson.JsonParseException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.text.ParseException;

import io.reactivex.functions.Consumer;

/**
 * Created by Snow on 2017/4/7.
 * Description:
 */

public class ErrorConsumer implements Consumer<Throwable> {
    private BaseView mBaseView;

    public ErrorConsumer(BaseView baseView) {
        this.mBaseView = baseView;
    }

    @Override
    public void accept(Throwable e) throws Exception {
        mBaseView.hideLoading();
        e.printStackTrace();
        if (e instanceof SocketTimeoutException
                || e instanceof ConnectException){
//                || e instanceof HttpException) {
            mBaseView.onError("-1", "网络太糟糕了!");
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            mBaseView.onError("-1", "服务器开小差了!");
        } else if (e instanceof com.jakewharton.retrofit2.adapter.rxjava2.HttpException) {
            mBaseView.onError("-1", "服务器错误!");
        } else {
            mBaseView.onError("-1", "神秘力量导致加载数据失败!");
        }
    }
}
